package com.yulian.util;

import io.vertx.core.json.JsonObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * TODO lb <类名>
 *
 */
public class DateUtils {


    //更加开票周期纬度 计算当前操作时间属于哪个纬度下的哪一类的区间范围 --从而得到开始时间和结束时间
    public static List<String> calculateDate(String cycle_name, JsonObject cycle_name_info) throws ParseException {
        List<String> dates =new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int date = calendar.get(Calendar.DATE);
        String star="";
        String end="";
        if (cycle_name.equals(cycle_name_info.getString("month"))){
            //自然月
         star=year+"-"+month+"-01 00:00:00";
         if (month==12){
             int year2=year+1;
             end=year2+"-01-01 00:00:00";
         }else {
             int month2=month+1;
             end=year+"-"+month2+"-01 00:00:00";
         }
        }else  if (cycle_name.equals(cycle_name_info.getString("natural_half_month"))){
            //自然半月
            if (date<=15){
                //前半月
                 star=year+"-"+month+"-01 00:00:00";
                 end=year+"-"+month+"-16 00:00:00";
            }else {
                //后半月
                if (month==12){
                    star=year+"-"+month+"-16 00:00:00";
                    int year2=year+1;
                    end=year2+"-01-01 00:00:00";
                }else {
                    star=year+"-"+month+"-16 00:00:00";
                    int month2=month+1;
                    end=year+"-"+month2+"-01 00:00:00";
                }
            }
        }else  if (cycle_name.equals(cycle_name_info.getString("natural_ten"))){
            //自然旬
            if (date<=10){
                //上旬
                star=year+"-"+month+"-01 00:00:00";
                end=year+"-"+month+"-11 00:00:00";
            }else if(date>10 && date <=20){
                //中旬
                star=year+"-"+month+"-11 00:00:00";
                end=year+"-"+month+"-21 00:00:00";
            }else {
                //下旬
                if (month==12){
                    star=year+"-"+month+"-21 00:00:00";
                    int year2=year+1;
                    end=year2+"-01-01 00:00:00";
                }else {
                    star=year+"-"+month+"-21 00:00:00";
                    int month2=month+1;
                    end=year+"-"+month2+"-01 00:00:00";
                }
            }
        }else  if (cycle_name.equals(cycle_name_info.getString("natural_week"))) {
            //自然周
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(new Date());
            int year0 = calendar2.get(Calendar.YEAR);
            int month0 = calendar2.get(Calendar.MONTH);
            int date0 = calendar2.get(Calendar.DATE);
            // 将Calendar对象的日期设置为本周的周一
            calendar2.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            calendar2.set(Calendar.HOUR_OF_DAY, 0);
            calendar2.set(Calendar.MINUTE, 0);
            calendar2.set(Calendar.SECOND, 0);
            calendar2.set(Calendar.MILLISECOND, 0);
            // 使用SimpleDateFormat类将Calendar对象的日期格式化为所需的字符串格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String monday = dateFormat.format(calendar2.getTime());
            int year1 = calendar2.get(Calendar.YEAR);
            int month1 = calendar2.get(Calendar.MONTH);
            int date1 = calendar2.get(Calendar.DATE);
            // 将Calendar对象的日期递增6天，以获取本周的周日
            calendar2.add(Calendar.DAY_OF_WEEK, 7);
            String sunday = dateFormat.format(calendar2.getTime());
            int year2 = calendar2.get(Calendar.YEAR);
            int month2 = calendar2.get(Calendar.MONTH);
            int date2 = calendar2.get(Calendar.DATE);
            if (year1!=year0){
                star=year0+"-01-01 00:00:00";
            }else if (month1!=month0){
                star=year0+"-"+month0+"-01 00:00:00";
            }else {
                star=year1+"-"+month1+"-"+date1+" 00:00:00";
            }

            if (year2!=year0){
                end=year2+"-01-01 00:00:00";
            }else if (month2!=month0){
                end=year0+"-"+month2+"-01 00:00:00";
            }else {
                end=year2+"-"+month2+"-"+date2+" 00:00:00";
            }
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateStar = format.parse(star);
        Date endStar = format.parse(end);
        dates.add(star);
        dates.add(end);
        return dates;
    }
}
